<template>
	<view class="content">
		<view class="navBox">
			<view :class="current==0?'navItems':'navItem'" @click="current=0;getList(true)">
				寄件地址管理
				<image v-show="current==0" src="../../static/image/choose.png" class="choose" mode=""></image>
			</view>
			<view :class="current==1?'navItems':'navItem'" @click="current=1;getList(false)">
				收件地址管理
				<image v-show="current==1" src="../../static/image/choose.png" class="choose" mode=""></image>
			</view>
		</view>
		<view class="list">
			<view style="height: 600rpx;" v-if="list.length==0">
				<u-empty text="暂无地址信息" mode="address"></u-empty>
			</view>
			<view class="addItem" v-for="(item,index) in list" v-else @click="chooseBtn(item)">
				<view class="left">
					<view class="top">
						<view class="name">{{item.name}}</view>
						<view class="phone">{{item.phone}}</view>
					</view>
					<view class="address">{{item.province}}-{{item.city}}-{{item.district}}-{{item.detail}}</view>
				</view>
				<view class="right">
					<image src="../../static/image/addEdit.png" class="edit" mode="" @click.stop="goInfo(`/pages-send/addressInfo/addressInfo?type=${current==0?1:0}&status=edit&id=${item.id}`)"></image>
					<image src="../../static/image/addDel.png" class="del" mode="" @click.stop="delBtn(item.id)"></image>
				</view>
			</view>
		</view>
		<view class="btn" @click="goInfo(`/pages-send/addressInfo/addressInfo?type=${current==0?1:0}&status=add`)">
			+ 新增寄件地址
		</view>
	</view>
</template>

<script>
	import {addressList,addressDel} from '../../common/api.js'
	export default {
		data() {
			return {
				current:0,
				list:[]
			}
		},
		onLoad(e) {
			console.log(e)
			this.current = e.type||0
			this.getList(this.current==0?true:false)
		},
		onShow() {
			this.getList(this.current==0?true:false)
		},
		methods: {
			chooseBtn(row){
				console.log(row)
				if(row.send){
					uni.$emit("jaddress", {
						data: row
					})
				}else if(!row.send){
					uni.$emit("saddress", {
						data: row
					})
				}
				
				uni.navigateBack({
					delta: 1
				});
			},
			getList(send){
				addressList({
					send
				}).then(res=>{
					console.log(res)
					if(res.code==10000){
						this.list = res.data
					}
				})
			},
			delBtn(id){
				let that = this
				uni.showModal({
					title: '提示',
					content: '是否确认删除此地址?',
					success: function (res) {
						if (res.confirm) {
							addressDel({id}).then(res=>{
								if(res.code==10000){
									uni.showToast({
										title: '删除成功',
										duration: 2000,
										icon:"none"
									});
									setTimeout(()=>{
										that.getList(that.current==0?true:false)
										
									},500)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
			},
			goInfo(url){
				uni.navigateTo({
					url:url
				})
			},
		}
	}
</script>

<style>
page{
	background: #F4F5F7;
}
</style>
<style scoped lang="scss">
	.content{
		width: 750rpx;
		padding: 32rpx;
		box-sizing: border-box;
		.navBox{
			width: 100%;
			padding-bottom: 50rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;
			.navItem{
				width: 50%;
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 32rpx;
				color: #333333;
				line-height: 32rpx;
				text-align: center;
				font-style: normal;
			}
			.navItems{
				width: 50%;
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #1AA261;
				line-height: 32rpx;
				text-align: center;
				font-style: normal;
				position: relative;
				.choose{
					width: 68rpx;
					height: 41rpx;
					position: absolute;
					bottom: -25rpx;
					left: 50%;
					transform: translateX(-50%);
				}
			}
			
			
		}
	
		.list{
			margin-top: 22rpx;
			.addItem{
				margin-bottom: 24rpx;
				width: 100%;
				height: 144rpx;
				background: #FFFFFF;
				border-radius: 20rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 40rpx;
				box-sizing: border-box;
				.left{
					.top{
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #333333;
						line-height: 28rpx;
						text-align: left;
						font-style: normal;
						display: flex;
						flex-wrap: nowrap;
						align-items: center;
						.phone{
							margin-left: 20rpx;
						}
					}
					.address{
						margin-top: 16rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #999999;
						line-height: 24rpx;
						text-align: left;
						font-style: normal;
					}
				}
				.right{
					display: flex;
					flex-wrap: nowrap;
					align-items: center;
					.edit,.del{
						width: 30rpx;
						height: 32rpx;
						margin-left: 20rpx;
					}
				}
			}
		}
		.btn{
			width: 90%;
			height: 88rpx;
			background: #1AA261;
			border-radius: 44rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 30rpx;
			color: #FFFFFF;
			line-height: 88rpx;
			text-align: center;
			font-style: normal;
			position: fixed;
			bottom: 30rpx;
			left: 50%;
			transform: translateX(-50%);
		}
	}
</style>
